package com.atguigu.jxc.service.impl;

import com.atguigu.jxc.dao.GoodsDao;
import com.atguigu.jxc.domain.ErrorCode;
import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.domain.SuccessCode;
import com.atguigu.jxc.entity.Goods;
import com.atguigu.jxc.entity.Log;
import com.atguigu.jxc.entity.Unit;
import com.atguigu.jxc.service.CustomerReturnListGoodsService;
import com.atguigu.jxc.service.GoodsService;
import com.atguigu.jxc.service.LogService;
import com.atguigu.jxc.service.SaleListGoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @description
 */
@Service
public class GoodsServiceImpl implements GoodsService {

    @Autowired
    private GoodsDao goodsDao;

    @Override
    public ServiceVO getCode() {

        // 获取当前商品最大编码
        String code = goodsDao.getMaxCode();

        // 在现有编码上加1
        Integer intCode = Integer.parseInt(code) + 1;

        // 将编码重新格式化为4位数字符串形式
        String unitCode = intCode.toString();

        for(int i = 4;i > intCode.toString().length();i--){

            unitCode = "0"+unitCode;

        }
        return new ServiceVO<>(SuccessCode.SUCCESS_CODE, SuccessCode.SUCCESS_MESS, unitCode);
    }

    // 首页库存列表查询
    @Override
    public Map<String, Object> listInventory(String page, String rows, String codeOrName, String goodsTypeId) {
//       if (page==null) page="";
//       if (rows==null) rows="";
//       if (goodsTypeId==null) goodsTypeId="";
       Integer pageNo=(Integer.parseInt(page)-1)*Integer.parseInt(rows);
       Integer size = Integer.parseInt(rows);
        List<Goods> goodsList = goodsDao.listInventory(pageNo,size,codeOrName,goodsTypeId);
        Map<String, Object> map = new HashMap<>();
        map.put("total",goodsList.size());
        map.put("rows",goodsList);
        return map;
    }

    // 所有商品单位
    @Override
    public Map unitlist() {
        List<Unit> unitList = goodsDao.unitList();
        Map map = new HashMap();
        map.put("rows",unitList);
        return map;
    }

    // 分页查询商品信息
    @Override
    public Map<String, Object> selectGoodsList(Integer page, Integer rows, String goodsName, String goodsTypeId) {
        List<Goods> goodsList = goodsDao.selectGoodsList(page,rows,goodsName,goodsTypeId);
        Map map = new HashMap();
        map.put("total",goodsList.size());
        map.put("rows",goodsList);
        return map;
    }

    // 保存或更新商品
    @Transactional
    @Override
    public void saveOrUpdate(Goods goods) {
        // 给inventoryQuantity一个默认值,state 0
        goods.setInventoryQuantity(0);
        goods.setState(0);
        if (goods.getGoodsId()!=null){
            goodsDao.updateById(goods);
        }else {

            goodsDao.save(goods);
        }
    }

    @Override
    public ServiceVO deleteGoodsById(Integer goodsId) {
        Goods goods = goodsDao.selectById(goodsId);
        if( goods.getState() == 2) {
            return new ServiceVO(ErrorCode.HAS_FORM_ERROR_CODE, ErrorCode.HAS_FORM_ERROR_MESS, null);
        }else  if(goods.getState() == 1 ){
            new ServiceVO(ErrorCode.STORED_ERROR_CODE, ErrorCode.STORED_ERROR_MESS, null);
        }else{
            goodsDao.deleteById(goodsId);
        }
            return new ServiceVO(SuccessCode.SUCCESS_CODE,SuccessCode.SUCCESS_MESS,null);

    }

    // 获取无库存商品列表
    @Override
    public Map getNoInventoryQuantity(Integer page, Integer rows, String nameOrCode) {
        List<Goods> goodsList = goodsDao.getNoInventoryQuantity(page,rows,nameOrCode);
        Map map = new HashMap();
        map.put("total",goodsList.size());
        map.put("rows",goodsList);
        return map;
    }

    @Override
    public Map getHasInventoryQuantity(Integer page, Integer rows, String nameOrCode) {
        List<Goods> goodsList = goodsDao.getHasInventoryQuantity(page,rows,nameOrCode);
        Map map = new HashMap();
        map.put("total",goodsList.size());
        map.put("rows",goodsList);
        return map;
    }

    //保存或更新库存
    @Override
    public void saveStock(Integer goodsId, Integer inventoryQuantity, Double purchasingPrice) {
        goodsDao.updateStockById(goodsId,inventoryQuantity,purchasingPrice);
    }

    // 查询所有 当前库存量 小于 库存下限的商品信息
    @Override
    public Map listAlarm() {
        List<Goods> goodsDaoList =goodsDao.listAlarm();
        Map map = new HashMap();
        map.put("rows",goodsDaoList);
        return map;
    }
}
